﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Module.Common.Models;
using Module.Core.BusinessLogic.CmsContents;
using Module.Core.DAO;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;


namespace Web.Cms.Users
{
    public partial class UserRoleZones : AdminUserControl
    {
        private int lang = 1;
        int roleID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {

            roleID = Convert.ToInt32((Request.QueryString["userinroleid"]));
            if (!Page.IsPostBack)
            {
                CMSZonesBL.GetSub(lisboxZone, "0", 1, 0, 0);
                CMSZonesBL.GetSub(listboxZoneUser, "0", 1, roleID, 0);
            }
            lang = Convert.ToInt32(dropLang.SelectedValue);
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "admin_usermanager", new string[] { }, new string[] { }));
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                bool _SaveSelected = true;
                CMSUserRoleZoneInfo list = new CMSUserRoleZoneInfo();
                DataSet ds = new DataSet();
                int ik = 0;
                for (int i = 0; i <= lisboxZone.Items.Count - 1; i++)
                {
                    if (lisboxZone.Items[i].Selected)
                    {
                        list.RoleID = Convert.ToInt32((Request.QueryString["userinroleid"]));
                        list.Lang = Convert.ToInt32(dropLang.SelectedValue);
                        list.ZoneIDLang = Convert.ToInt32(lisboxZone.Items[i].Value);
                        list.CheckRole = 0;
                        CHRTList<CMSUserRoleZoneInfo> listCheck =
                            CMSUserRoleZoneBL.CheckUserRoleZone(Convert.ToInt32((Request.QueryString["userinroleid"])),
                                                                Convert.ToInt32(lisboxZone.Items[i].Value), 1);
                        if (listCheck != null && listCheck.Count > 0)
                        {
                            if (listCheck[0].ZoneIDLang == Convert.ToInt32(lisboxZone.Items[i].Value))
                                _SaveSelected = false;
                        }
                        if (_SaveSelected == true)
                        {
                            ik = CMSUserRoleZoneBL.Insert(list);
                        }
                        _SaveSelected = true;
                    }
                }
                CMSZonesBL.GetSub(listboxZoneUser, "0", 1, roleID, 0);
                lbtb.Text = Definitions.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lbtb.Text = ex.Message;
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                CMSUserRoleZoneInfo list = new CMSUserRoleZoneInfo();
                for (int i = 0; i <= listboxZoneUser.Items.Count - 1; i++)
                {
                    if (listboxZoneUser.Items[i].Selected)
                    {
                        list.RoleID = Convert.ToInt32((Request.QueryString["userinroleid"]));
                        list.ZoneIDLang = Convert.ToInt32(listboxZoneUser.Items[i].Value);
                        list.Lang = 1;
                        CMSUserRoleZoneBL.Delete(list);
                    }
                }

                CMSZonesBL.GetSub(listboxZoneUser, "0", 1, roleID, 0);
                lbtb.Text = Definitions.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                lbtb.Text = ex.Message;
            }
        }   
    }
}